<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Ashop extends Base
{

	function __construct()
	{
	}

	public function create($param)
	{
		$name = isset($param['name']) ? trim($param['name']):'';
		$contacts = isset($param['contacts']) ? trim($param['contacts']):'';
		$account= isset($param['account']) ? trim($param['account']):'';
		$phone = isset($param['phone']) ? trim($param['phone']):'';
		$province = isset($param['province']) ? trim($param['province']):'';
		$city = isset($param['city']) ? trim($param['city']):'';
		$area = isset($param['area']) ? trim($param['area']):'';
		$address = isset($param['address']) ? trim($param['address']):'';
        $password = isset($param['password']) ? trim($param['password']):'';
        $teacher_num = isset($param['teacher_num']) ? trim($param['teacher_num']):0;
        $is_group = isset($param['is_group']) ? trim($param['is_group']):0;
        $group_store = isset($param['group_store']) ? trim($param['group_store']):'';
        $start_time = isset($param['start_time']) ? trim($param['start_time']):'';
		$end_time = isset($param['end_time']) ? trim($param['end_time']):'';
		if (!$name || !$contacts || !$phone || !$address || !$password || !$start_time || !$end_time) {
			$this->res_data['Msg'] = "缺少必填信息";
			return $this->res_data;
		}
        $password=get_admin_pwd($password);

		$data['name'] =$name;
		$data['contacts'] =$contacts;
		$data['account'] =$account;
		$data['phone'] =$phone;
		$data['province'] =$province;
		$data['city'] =$city;
		$data['store_photos'] ='';
		$data['area'] =$area;
        $data['address'] =$address;
        $data['teacher_num'] =$teacher_num;
        $data['is_group'] =$is_group;
		$data['group_store'] =$group_store;
        $data['password'] =$password;
        $data['start_time'] =strtotime($start_time);
		$data['end_time'] =strtotime($end_time);
		$data['createtime'] =time();
		$data['updatetime'] =time();
        Db::startTrans();
        // try {
            $sdata = Db::name('store')->insertGetId($data);
            if(!$sdata) {
                Db::rollBack();
                $this->res_data['Msg'] = '添加门店失败';
                return $this->res_data;
            }
            // 创建默认轮播图、学员风采、教室、教师、班级、课程
            $store = Db::name('store')->where(['name'=>'默认模板'])->find();
            if ($store) {
                $store_data['pic1'] = $store['pic1'];
                $store_data['pic2'] = $store['pic2'];
                $store_data['pic3'] = $store['pic3'];
                $store_data['store_photos'] = $store['store_photos'];
                $store_data['updatetime'] = time();
                $is_update1 = Db::name('store')->where(['id'=>$sdata])->update($store_data);
                if(!$is_update1) {
                    Db::rollBack();
                    $this->res_data['Msg'] = '增加默认门店数据失败';
                    return $this->res_data;
                }
                $classroom = Db::name('classroom')->where(['sid'=>$store['id']])->find();
                if ($classroom) {
                    $classroom_data['name'] = $classroom['name'];
                    $classroom_data['status'] = 1;
                    $classroom_data['createtime'] = time();
                    $classroom_data['sid'] = $sdata;
                    $is_insert2 = Db::name('classroom')->insertGetId($classroom_data);
                    if(!$is_insert2) {
                        Db::rollBack();
                        $this->res_data['Msg'] = '增加默认教室数据失败';
                        return $this->res_data;
                    }
                }
                $teacher = Db::name('teacher')->where(['sid'=>$store['id']])->select();
                $teacher_id_arr = [];
                if ($teacher) {
                    foreach ($teacher as $key => $value) {
                        $user = Db::name('user')->where('id',$value['user_id'])->find();
                        $user_data = $user;
                        unset($user_data['id']);
                        $user_data['sid'] = $sdata;
                        $user_data['create_time'] = time();
                        $user_data['update_time'] = 0;
                        $is_insert3 = Db::name('user')->insertGetId($user_data);
                        if(!$is_insert3) {
                            Db::rollBack();
                            $this->res_data['Msg'] = '增加默认老师数据失败';
                            return $this->res_data;
                        }
                        $teacher_data = $value;
                        unset($teacher_data['id']);
                        $teacher_data['sid'] = $sdata;
                        $teacher_data['createtime'] = time();
                        $teacher_data['updatetime'] = 0;
                        $is_insert4 = Db::name('teacher')->insertGetId($teacher_data);
                        if(!$is_insert4) {
                            Db::rollBack();
                            $this->res_data['Msg'] = '增加默认老师数据失败';
                            return $this->res_data;
                        }
                        $teacher_id_arr[] = $is_insert4;
                    }
                }
                $classes = Db::name('classes')->where(['sid'=>$store['id']])->find();
                if ($classes) {
                    $classes_data['name'] = $classes['name'];
                    if (isset($is_insert4)) {
                        $classes_data['master_teacher'] = $is_insert4;
                        $classes_data['assistant_teacher'] = $is_insert4;
                    }
                    $classes_data['status'] = 1;
                    $classes_data['createtime'] = time();
                    $classes_data['sid'] = $sdata;
                    $is_insert5 = Db::name('classes')->insertGetId($classes_data);
                    if(!$is_insert5) {
                        Db::rollBack();
                        $this->res_data['Msg'] = '增加默认班级数据失败';
                        return $this->res_data;
                    }
                }
                $course = Db::name('course')->where(['sid'=>$store['id']])->select();
                if ($course) {
                    foreach ($course as $key2 => $value2) {
                        $course_data = $value2;
                        unset($course_data['id']);
                        $course_data['sid'] = $sdata;
                        $course_data['createtime'] = time();
                        $course_data['updatetime'] = 0;
                        if (isset($is_insert4)) {
                            $course_data['teacher_id'] = $is_insert4;
                            $course_data['teacher_name'] = $teacher_data['name'];
                        }
                        if (isset($is_insert2)) {
                            $course_data['classroom_id'] = $is_insert2;
                            $course_data['classroom_name'] = $classroom_data['name'];
                        }
                        if (isset($is_insert5)) {
                            $course_data['classes_id'] = $is_insert5;
                            $course_data['classes_name'] = $classes_data['name'];
                        }
                        $is_insert6 = Db::name('course')->insertGetId($course_data);
                        if(!$is_insert6) {
                            Db::rollBack();
                            $this->res_data['Msg'] = '增加默认课程数据失败';
                            return $this->res_data;
                        }
                        // 课时
                        $class_time = $course_data['class_time'];
                        $periods = $course_data['periods'];
                        $cid = $is_insert6;
                        $week_arr = [];
                        $start_time_arr = [];
                        $end_time_arr = [];
                        if ($class_time) {
                            $class_time_arr = explode('|', $class_time);
                            foreach ($class_time_arr as $k => $v) {
                                $class_time_arr_arr = explode(',', $v);
                                $week_arr[] = $class_time_arr_arr[0];
                                $start_time_arr[] = $class_time_arr_arr[1];
                                $end_time_arr[] = $class_time_arr_arr[2];
                            }
                        }
                        $CourseLogic = Controller('course','logic');
                        $res = $CourseLogic->save_classtime($week_arr,$start_time_arr,$end_time_arr,$periods,$cid);
                        if (!$res) {
                            Db::rollback();
                            $this->res_data['Msg'] = '增加默认课程数据失败';
                            return $this->res_data;
                        }
                    }
                }
            }
            Db::commit();
            $url = config("http_domain").'dboard/tqr/store/sid/'.$sdata;
            $at =file_get_contents($url);
            $res = json_decode($at,1);
            if (!isset($res['Code']) || $res['Code']!=0) {
                $this->res_data['Msg'] = '增加门店二维码失败';
                return $this->res_data;
            }
            if (isset($teacher_id_arr) && $teacher_id_arr) {
                foreach ($teacher_id_arr as $arr) {
                    $url = config("http_domain").'dboard/tqr/teacher/tid/'.$arr;
                    $at =file_get_contents($url);
                    $res = json_decode($at,1);
                    if (!isset($res['Code']) || $res['Code']!=0) {
                        $this->res_data['Msg'] = '增加默认老师二维码失败';
                        return $this->res_data;
                    }
                }
            }
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        // } catch (\Exception $e) {
        //     Db::rollBack();
        //     $this->res_data['Msg'] = '添加门店失败';
        //     return $this->res_data;
        // }
	}
	public function edit($param)
	{
		$name = isset($param['name']) ? trim($param['name']):'';
		$contacts = isset($param['contacts']) ? trim($param['contacts']):'';
		$account = isset($param['account']) ? trim($param['account']):'';
		$phone = isset($param['phone']) ? trim($param['phone']):'';
		$password = isset($param['password']) ? trim($param['password']):'';
		$province = isset($param['province']) ? trim($param['province']):'';
		$city = isset($param['city']) ? trim($param['city']):'';
		$area = isset($param['area']) ? trim($param['area']):'';
		$address = isset($param['address']) ? trim($param['address']):'';
        $sid = isset($param['sid']) ? intval($param['sid']):0;
		$teacher_num = isset($param['teacher_num']) ? intval($param['teacher_num']):0;
        $is_group = isset($param['is_group']) ? trim($param['is_group']):0;
        $group_store = isset($param['group_store']) ? trim($param['group_store']):'';
        $start_time = isset($param['start_time']) ? trim($param['start_time']):'';
        $end_time = isset($param['end_time']) ? trim($param['end_time']):'';
		if (!$sid || !$name || !$contacts || !$phone || !$address || !$start_time || !$end_time) {
			$this->res_data['Msg'] = "缺少必填信息";
			return $this->res_data;
		}
		$store= Db::name('store')->where([['id','=',$sid]])->find();
		if (!$store) {
			$this->res_data['Msg'] = "无此门店";
			return $this->res_data;
		}

		$data['name'] =$name;
		$data['contacts'] =$contacts;
		$data['account'] =$account;
		$data['phone'] =$phone;
		$data['province'] =$province;
		$data['city'] =$city;
		$data['area'] =$area;
        $data['address'] =$address;
		$data['teacher_num'] =$teacher_num;
        $data['is_group'] =$is_group;
        $data['group_store'] =$group_store;
        $data['start_time'] =strtotime($start_time);
        $data['end_time'] =strtotime($end_time);
        if($password){
            $password=get_admin_pwd($password);
		    $data['password'] =$password;
            $data['token'] = '';
        }
		$data['updatetime'] =time();
        $sdata = Db::name('store')->where([['id','=',$sid]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
	}
	public function updateStore($param)
	{
		$sid = isset($param['sid']) ? intval($param['sid']):0;
		$status = isset($param['status']) ? intval($param['status']):0;
		$store= Db::name('store')->where([['id','=',$sid]])->find();
		if (!$store) {
			$this->res_data['Msg'] = "无此门店";
			return $this->res_data;
		}
		$data['status'] =$status;
		$data['updatetime'] =time();
        $sdata = Db::name('store')->where([['id','=',$sid]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '操作失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
	}
    //门店页信息
    public function storeInfo($param)
    {   
        $sid = isset($param['sid'])?intval($param['sid']):0;
        if (!$sid){
            $this->res_data['Msg'] = "门店过期或无此门店";
            return $this->res_data;
        }
        $info= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$info){
            $this->res_data['Msg'] = "门店过期或无此门店";
            return $this->res_data;
        }
        $info['start_time'] = date('Y-m-d H:i:s',$info['start_time']);
        $info['end_time'] = date('Y-m-d H:i:s',$info['end_time']);

        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        $this->res_data["Data"] = ['store'=>$info];
        return $this->printResult($this->res_data);
    }

}

?>
